package com.cmc.cloud.cmclink.doc.service.impl.so;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.SoCargoErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.convert.SoCargoConvert;
import com.cmc.cloud.cmclink.doc.entity.SoCargoDO;
import com.cmc.cloud.cmclink.doc.mapper.SoCargoMapper;
import com.cmc.cloud.cmclink.doc.service.so.SoCargoService;
import com.cmc.cloud.cmclink.doc.vo.sovo.socargovo.*;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 订舱货物信息 Service 实现类
 *
 * @author 招商轮船-管理员
 */
@Service
@Validated
public class SoCargoServiceImpl implements SoCargoService {

    @Resource
    private SoCargoMapper soCargoMapper;

    @Override
    public void createSoCargo(SoCargoCreateReqVO createReqVO) {
        // 插入
        SoCargoDO soCargo = SoCargoConvert.INSTANCE.convert(createReqVO);
        soCargoMapper.insert(soCargo);
    }

    @Override
    public void createOrUpdateAndDeleteBatch(Long soId, List<SoCargoUpdateReqVO> newList, List<SoCargoRespVO> oldList) {
        // 无效数据直接返回
        if (CollectionUtil.isEmpty(newList) && CollectionUtil.isEmpty(oldList)) {
            return;
        }
        // 默认值
        if (CollectionUtil.isEmpty(newList)) {
            newList = new ArrayList<>();
        }
        if (CollectionUtil.isEmpty(oldList)) {
            oldList = new ArrayList<>();
        }
        List<SoCargoUpdateReqVO> createList = new ArrayList<>();
        List<SoCargoUpdateReqVO> updateList = new ArrayList<>();
        // 历史数据
        Set<Long> oldIds = oldList.stream().map(e -> e.getId()).collect(Collectors.toSet());
        newList.forEach(e -> {
            if (e.getId() != null) {
                if (oldIds.contains(e.getId())) {
                    // 正常更新
                    updateList.add(e);
                    oldIds.remove(e.getId()); // 剩下的数据需要删除
                } else {
                    // 数据更新非法
                    throw ServiceExceptionUtil.exception(SoCargoErrorCodeConstants.SO_CARGO_NOT_EXISTS);
                }
            } else {
                e.setSoId(soId);
                createList.add(e);
            }
        });
        if (CollectionUtil.isNotEmpty(oldIds)) {
            // 没有匹配上的就是前端删除的
            soCargoMapper.deleteBatchIds(oldIds);
        }
        if (CollectionUtil.isNotEmpty(createList)) {
            List<SoCargoDO> soCargoCreateList = SoCargoConvert.INSTANCE.convertBatchUpdate(createList);
            soCargoMapper.insertBatch(soCargoCreateList);
        }
        if (CollectionUtil.isNotEmpty(updateList)) {
            List<SoCargoDO> soVslvoyUpdateList = SoCargoConvert.INSTANCE.convertBatchUpdate(updateList);
            soCargoMapper.updateBatch(soVslvoyUpdateList);
        }

    }

    @Override
    public void createBatch(List<SoCargoCreateReqVO> createReqVOList) {
        // 插入
        List<SoCargoDO> list = SoCargoConvert.INSTANCE.convertBatch(createReqVOList);
        soCargoMapper.insertBatch(list);
    }

    @Override
    public List<SoCargoSumVO> selectSumBySoIds(List<Long> soIds) {
        if (CollectionUtil.isEmpty(soIds)) {
            return new ArrayList<>();
        }
        return soCargoMapper.selectSumBySoIds(soIds);
    }

    @Override
    public void updateSoCargo(SoCargoUpdateReqVO updateReqVO) {
        // 校验存在
        validateSoCargoExists(updateReqVO.getId());
        // 更新
        SoCargoDO updateObj = SoCargoConvert.INSTANCE.convert(updateReqVO);
        soCargoMapper.updateById(updateObj);
    }

    @Override
    public void deleteSoCargo(Long id) {
        // 校验存在
        validateSoCargoExists(id);
        // 删除
        soCargoMapper.deleteById(id);
    }

    private void validateSoCargoExists(Long id) {
        if (soCargoMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(SoCargoErrorCodeConstants.SO_CARGO_NOT_EXISTS);
        }
    }

    @Override
    public SoCargoDO getSoCargo(Long id) {
        return soCargoMapper.selectById(id);
    }

    @Override
    public List<SoCargoDO> getSoCargoList(Collection<Long> ids) {
        return soCargoMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<SoCargoDO> getSoCargoPage(SoCargoPageReqVO reqVO) {
        LambdaQueryWrapperX<SoCargoDO> queryWrapper = new LambdaQueryWrapperX<>();
        return soCargoMapper.selectPage(reqVO, queryWrapper);
    }

    @Override
    public void deleteBySoIds(Collection<Long> sIds) {
        LambdaUpdateWrapper<SoCargoDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(SoCargoDO::getSoId, sIds);
        soCargoMapper.delete(updateWrapper);
    }

    @Override
    public List<SoCargoDO> seleteBySoId(Long soId) {
        LambdaQueryWrapper<SoCargoDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SoCargoDO::getSoId, soId);
        queryWrapper.orderByAsc(SoCargoDO::getId);
        return soCargoMapper.selectList(queryWrapper);
    }

    @Override
    public Map<Long, SoCargoDO> selectBySoIds(List<Long> soIds) {
        LambdaQueryWrapper<SoCargoDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SoCargoDO::getSoId, soIds);
        return soCargoMapper.selectList(queryWrapper).stream().collect(Collectors.toMap(SoCargoDO::getSoId, e -> e));
    }
}
